import heapq
class Solution(object):
    def getNumberOfBacklogOrders(self, orders):
        buy_st = []
        sell_st = []
        for p, m, t in orders:
            if t == 0:
                while sell_st and sell_st[0][0] <= p and m > 0:
                    x, y = heapq.heappop(sell_st)
                    if y <= m:
                        m -= y
                    else:
                        heapq.heappush(sell_st, [x, y - m])
                        m = 0
                if m > 0:
                    heapq.heappush(buy_st, [- p, m])
            else:
                while buy_st and - buy_st[0][0] >= p and m > 0:
                    x, y = heapq.heappop(buy_st)
                    if y <= m:
                        m -= y
                    else:
                        heapq.heappush(buy_st, [x, y - m])
                        m = 0
                if m > 0:
                    heapq.heappush(sell_st, [p, m])
        ans = 0
        for _, m in buy_st:
            ans += m
        for _, m in sell_st:
            ans += m
        return ans % (10 ** 9 + 7)